import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img_rgb = cv.imread('../../data/mario.png')
img_org = img_rgb.copy()
print(img_rgb.shape)
img_gray =cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)
print(img_gray.shape)
template=cv.imread('../../data/mario_coin.png',0)
print(template.shape)
w,h=template.shape[::-1]

res =cv.matchTemplate(img_gray,template,cv.TM_CCOEFF_NORMED)

threshold =0.9
loc =np.where( res >= threshold)
cordXs,cordYs = loc[1],loc[0]
for idx,X in enumerate(cordXs):
    cv.rectangle(img_rgb,(X,cordYs[idx]),(X+w,cordYs[idx]+ h),(0,0,255),1)
cv.imshow("res",res)
cv.imshow("img_org",img_org)
cv.imshow("img_rgb",img_rgb)
cv.waitKey(0)
cv.destroyAllWindows()